<template>
	<!-- 游客管理 -->
	<view class="container">
		<!-- 头部导航 -->
		<page-navbar :title="isSelect ? '选择成员' : '成员列表'" :showBack="true">
		</page-navbar>

		<!-- 主要内容区域 -->
		<scroll-view class="content" scroll-y="true" :show-scrollbar="false">
			<!-- 选择模式顶部操作栏 -->
			<view class="select-header" v-if="isSelect">
				<view class="select-actions">
					<view class="select-all" @click="toggleSelectAll">
						<view class="checkbox" :class="{ checked: isAllSelected }">
							<text class="check-icon" v-if="isAllSelected">✓</text>
						</view>
						<text class="select-text">全选</text>
					</view>
					<view class="selected-count">
						<text>已选择 {{ selectedCount }} 人</text>
					</view>
				</view>
			</view>

			<!-- 游客列表 -->
			<view class="visitor-list">
				<view class="visitor-item" v-for="(visitor, index) in allVisitors" :key="visitor.id"
					@click="handleVisitorClick(visitor)">

					<!-- 多选框（选择模式下显示） -->
					<view class="checkbox-wrapper" v-if="isSelect">
						<view class="checkbox" :class="{ checked: isVisitorSelected(visitor.id) }">
							<text class="check-icon" v-if="isVisitorSelected(visitor.id)">✓</text>
						</view>
					</view>

					<!-- 游客信息 -->
					<view class="visitor-info">
						<view class="visitor-header">
							<text class="visitor-label">游客{{ index + 1 }}</text>
							<view class="visitor-name">{{ visitor.name }}</view>
						</view>
						<view class="visitor-details">
							<view class="detail-row">
								<text class="detail-label">身份证：</text>
								<text class="detail-value">{{ visitor.idCard }}</text>
							</view>
							<view class="detail-row">
								<text class="detail-label">手机号：</text>
								<text class="detail-value">{{ visitor.phone }}</text>
							</view>
						</view>
					</view>

					<!-- 删除按钮（非选择模式下显示） -->
					<view class="delete-btn" @click.stop="confirmDelete(visitor)">
						<text class="delete-icon">×</text>
					</view>
				</view>
			</view>

			<!-- 添加游客按钮 -->
			<view class="add-visitor" @click="addNewVisitor">
				<view class="add-btn">
					<text class="add-icon">+</text>
					<text class="add-text">添加游客</text>
				</view>
			</view>

			<!-- 底部占位 -->
			<view class="bottom-placeholder"></view>
		</scroll-view>

		<!-- 底部操作栏（选择模式） -->
		<view class="bottom-action" v-if="isSelect">
			<view class="action-buttons">
				<button class="cancel-btn" @click="cancelSelect">取消</button>
				<button class="confirm-btn" @click="confirmSelect" :disabled="selectedCount === 0">
					确定 ({{ selectedCount }})
				</button>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
import useSelectStore from '@/stores/select.js';
import { storeToRefs } from 'pinia';
import api from "@/api"

const selectStore = useSelectStore();
const { selectedVisitors } = storeToRefs(selectStore);

// 页面参数
const isSelect = ref(true);
const isAllSelected = ref(false);

const allVisitors = ref([])

// 计算属性
const selectedCount = computed(() => {
	isAllSelected.value = selectedVisitors.value.length > 0 && selectedVisitors.value.length === allVisitors.value.length;
	return selectedVisitors.value.length;
});

// 检查游客是否被选中
const isVisitorSelected = (id) => {
	return selectedVisitors.value.some(visitor => visitor.id === id);
};

onShow(() => {
	getData();
})

onLoad((options) => {
	// 根据路由参数判断是否为选择模式
	if (options.type == 'select') {
		isSelect.value = true;
	} else {
		isSelect.value = false;
	}
});

onMounted(() => { });

const getData = () => {
	api.userApi.listTourist().then(res => {
		console.log('游客列表', res);
		allVisitors.value = res
	})
}

// 切换全选状态
const toggleSelectAll = () => {
	if (isAllSelected.value) {
		// 取消全选
		selectedVisitors.value = []
		isAllSelected.value = false;
	} else {
		// 全选
		selectedVisitors.value = [...allVisitors.value]
		isAllSelected.value = true;
	}
};

// 处理游客点击事件
const handleVisitorClick = (visitor) => {
	if (isSelect.value) {
		const currentSelected = [...selectedVisitors.value];
		const index = currentSelected.findIndex(v => v.id === visitor.id);

		if (index > -1) {
			// 取消选择
			currentSelected.splice(index, 1);
		} else {
			// 选择
			currentSelected.push(visitor);
		}
		selectedVisitors.value = currentSelected;
	}
};


// 确认删除游客
const confirmDelete = (visitor) => {
	uni.showModal({
		title: '确认删除',
		content: `确定要删除游客"${visitor.name}"吗？删除后无法恢复。`,
		confirmText: '删除',
		confirmColor: '#ff4757',
		cancelText: '取消',
		success: (res) => {
			if (res.confirm) {
				console.log(visitor);
				api.userApi.deleteTourist({ id: visitor.id }).then(res => {
					console.log(res);
					uni.showToast({
						title: '删除成功',
						icon: 'success'
					});

					getData();
				})
			}
		}
	});
};

// 添加新游客
const addNewVisitor = () => {
	uni.navigateTo({
		url: '/pages/details/vitorsAdd'
	});
};



// 取消选择
const cancelSelect = () => {
	selectStore.updateSelectedVisitors([]);
	isSelect.value = false;

	// 如果是从其他页面跳转过来的选择模式，返回上一页
	uni.navigateBack();
};

// 确认选择
const confirmSelect = () => {
	if (selectedCount.value === 0) {
		uni.showToast({
			title: '请选择游客',
			icon: 'none'
		});
		return;
	}

	// 更新状态管理中的选中游客
	selectStore.selectedVisitors = [...selectedVisitors.value];

	uni.showToast({
		title: `已选择${selectedCount.value}位游客`,
		icon: 'success'
	});

	// 返回上一页
	setTimeout(() => {
		uni.navigateBack();
	}, 1000);
};
</script>

<style lang="scss" scoped>
.container {
	background-color: #f5f5f5;
	min-height: 100vh;
	width: 100%;
	display: flex;
	flex-direction: column;
}

.nav-right {
	padding: 10rpx 20rpx;
	box-sizing: border-box;

	.nav-text {
		color: white;
		font-size: 28rpx;
	}
}

.content {
	flex: 1;
	padding: 20rpx;
	padding-bottom: 120rpx;
	box-sizing: border-box;
}

/* 选择模式头部 */
.select-header {
	background: white;
	border-radius: 12rpx;
	padding: 20rpx 30rpx;
	margin-bottom: 20rpx;
	box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);

	.select-actions {
		display: flex;
		justify-content: space-between;
		align-items: center;

		.select-all {
			display: flex;
			align-items: center;

			.select-text {
				margin-left: 15rpx;
				font-size: 28rpx;
				color: #333;
			}
		}

		.selected-count {
			font-size: 26rpx;
			color: #56ab2f;
			font-weight: 500;
		}
	}
}

/* 游客列表 */
.visitor-list {
	.visitor-item {
		background: white;
		border-radius: 12rpx;
		padding: 25rpx;
		margin-bottom: 15rpx;
		display: flex;
		align-items: center;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.08);
		transition: all 0.3s ease;

		&:active {
			transform: scale(0.98);
			box-shadow: 0 4rpx 15rpx rgba(0, 0, 0, 0.12);
		}

		.checkbox-wrapper {
			margin-right: 20rpx;
		}

		.visitor-info {
			flex: 1;

			.visitor-header {
				display: flex;
				align-items: center;
				margin-bottom: 15rpx;

				.visitor-label {
					background: linear-gradient(135deg, #56ab2f, #a8e6cf);
					color: white;
					font-size: 22rpx;
					padding: 6rpx 12rpx;
					border-radius: 12rpx;
					margin-right: 15rpx;
					font-weight: 500;
				}

				.visitor-name {
					font-size: 28rpx;
					font-weight: bold;
					color: #333;
				}
			}

			.visitor-details {
				.detail-row {
					display: flex;
					align-items: center;
					margin-bottom: 8rpx;

					&:last-child {
						margin-bottom: 0;
					}

					.detail-label {
						font-size: 24rpx;
						color: #666;
						width: 120rpx;
						flex-shrink: 0;
					}

					.detail-value {
						font-size: 24rpx;
						color: #333;
						flex: 1;
					}
				}
			}
		}

		.delete-btn {
			width: 50rpx;
			height: 50rpx;
			background: linear-gradient(135deg, #ff6b6b, #ff8e8e);
			border-radius: 50%;
			display: flex;
			align-items: center;
			justify-content: center;
			box-shadow: 0 4rpx 12rpx rgba(255, 107, 107, 0.3);

			.delete-icon {
				color: white;
				font-size: 28rpx;
				font-weight: bold;
			}
		}
	}
}

/* 多选框样式 */
.checkbox {
	width: 40rpx;
	height: 40rpx;
	border: 2rpx solid #ddd;
	border-radius: 8rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	transition: all 0.3s ease;

	&.checked {
		background: linear-gradient(135deg, #56ab2f, #a8e6cf);
		border-color: #56ab2f;

		.check-icon {
			color: white;
			font-size: 24rpx;
			font-weight: bold;
		}
	}
}

/* 添加游客按钮 */
.add-visitor {
	margin-top: 30rpx;

	.add-btn {
		background: linear-gradient(135deg, #56ab2f, #a8e6cf);
		border-radius: 12rpx;
		padding: 24rpx 30rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		box-shadow: 0 4rpx 15rpx rgba(86, 171, 47, 0.3);
		transition: all 0.3s ease;

		&:active {
			transform: scale(0.98);
		}

		.add-icon {
			color: white;
			font-size: 32rpx;
			font-weight: bold;
			margin-right: 10rpx;
		}

		.add-text {
			color: white;
			font-size: 28rpx;
			font-weight: 500;
		}
	}
}

.bottom-placeholder {
	height: 120rpx;
}

/* 底部操作栏 */
.bottom-action {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	background: white;
	padding: 20rpx 30rpx;
	padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
	border-top: 1rpx solid #eee;
	box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.1);

	.action-buttons {
		display: flex;
		gap: 20rpx;

		.cancel-btn {
			flex: 1;
			height: 80rpx;
			background: #f5f5f5;
			color: #666;
			border: none;
			border-radius: 40rpx;
			font-size: 28rpx;
			display: flex;
			align-items: center;
			justify-content: center;

			&:active {
				background: #e8e8e8;
			}
		}

		.confirm-btn {
			flex: 2;
			height: 80rpx;
			background: linear-gradient(135deg, #56ab2f, #a8e6cf);
			color: white;
			border: none;
			border-radius: 40rpx;
			font-size: 28rpx;
			font-weight: bold;
			display: flex;
			align-items: center;
			justify-content: center;
			box-shadow: 0 4rpx 15rpx rgba(86, 171, 47, 0.3);

			&:disabled {
				background: #ccc;
				box-shadow: none;
			}

			&:active:not(:disabled) {
				transform: scale(0.98);
			}
		}
	}
}
</style>
